package com.ejlchina.tool.activity

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.InputType
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.widget.LinearLayout
import android.widget.TextView
import com.adolf.androiddemo.R
import com.adolf.androiddemo.databinding.ActivityLoginBinding
import com.adolf.androiddemo.databinding.TestBottomDialogBinding
import com.ejlchina.tool.MainActivity
import com.ejlchina.tool.MyApplication
import com.ejlchina.tool.base.BaseBindingActivity
import com.ejlchina.tool.constant.SP_MECHANISM
import com.ejlchina.tool.constant.SP_PASSWORD
import com.ejlchina.tool.constant.SP_USER_NAME
import com.ejlchina.tool.network.NetHttpManager
import com.ejlchina.tool.singleton.SharedPreferenceUtil
import com.ejlchina.tool.util.getStatusBarHeight
import com.ejlchina.tool.util.showToast
import com.ejlchina.tool.util.transparentStatusBar
import com.google.android.material.bottomsheet.BottomSheetDialog
import kotlin.properties.Delegates

class LoginActivity: BaseBindingActivity<ActivityLoginBinding>() {

    private var isShowPassword :Boolean by Delegates.observable(false){ _,_,newValue ->
        setPasswordHideOrShow(newValue)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initActionBar()
        initViews()
    }

    private fun initViews(){
        setLoginData()
        binding.btnConfirm.setOnClickListener {
            login()
        }

        binding.ivPasswordShow.setOnClickListener{
            isShowPassword = !isShowPassword
        }
    }

    private fun setLoginData(){
        binding.etPlatform.setText(SharedPreferenceUtil.getString(SP_MECHANISM,""))
        binding.etUsername.setText(SharedPreferenceUtil.getString(SP_USER_NAME,""))
        binding.etPassword.setText(SharedPreferenceUtil.getString(SP_PASSWORD,""))
    }

    private fun showBottom(){
        val layoutBinding = TestBottomDialogBinding.inflate(layoutInflater,null,false)
        val bottomSheetDialog = BottomSheetDialog(this, R.style.bottom_sheet_dialog)
        bottomSheetDialog.setContentView(layoutBinding.root)
        bottomSheetDialog.show()
    }

    private fun login(){
        showLoading()
//        NetHttpManager.getInstance().doLogin(
//            binding.etPlatform.text.toString(),
//            binding.etUsername.text.toString(),
//            binding.etPassword.text.toString()){ errorMessage ->
//            dismissLoading()
//            errorMessage?.let {
//                showToast(it)
//            } ?: run {
//                SharedPreferenceUtil.putString(SP_MECHANISM,binding.etPlatform.text.toString())
//                SharedPreferenceUtil.putString(SP_USER_NAME,binding.etUsername.text.toString())
//                SharedPreferenceUtil.putString(SP_PASSWORD,binding.etPassword.text.toString())
//                startActivity(Intent(this,MainActivity::class.java))
//                finish()
//            }
//        }
    }

    private fun setPasswordHideOrShow(isSHowPassword: Boolean){
        val method = if (isSHowPassword) HideReturnsTransformationMethod.getInstance() else PasswordTransformationMethod.getInstance()
        val res = if (isSHowPassword) R.mipmap.ic_password_hide else R.mipmap.ic_password_show
        binding.etPassword.transformationMethod = method
        binding.ivPasswordShow.setImageResource(res)
        binding.etPassword.setSelection(binding.etPassword.text.toString().length)
    }
    override fun getViewBinding(): ActivityLoginBinding {
        return ActivityLoginBinding.inflate(layoutInflater)
    }

    private fun initActionBar(){
        getStatusBarHeight(this)
        transparentStatusBar(window)
    }
}